if not IsAddOnLoaded("ElvUI") then return end
if not IsAddOnLoaded("DXE") then return end
local skin = CreateFrame("Frame")
skin:RegisterEvent("ADDON_LOADED")
skin:SetScript("OnEvent", function(self, event, addon)
	if addon ~= "DXE" then return end
	if (UISkinOptions.DXESkin == "Disabled") then return end
	local s = U.s
	local c = U.c

local DXE = DXE
local _G = getfenv(0)
local barSpacing = 1
local borderWidth = 2
local buttonZoom = {.09,.91,.09,.91}
local movers = {
	"DXEAlertsCenterStackAnchor",
	"DXEAlertsWarningStackAnchor",
	"DXEDistributorStackAnchor",
	"DXEAlertsTopStackAnchor",
	"DXEArrowsAnchor1",
	"DXEArrowsAnchor2",
	"DXEArrowsAnchor3",
}

local c = {}
function SkinRWIcon(addon, text, r, g, b, _, _, _, _, _, icon)
	if not c[r] then c[r] = {} end
	if not c[r][g] then c[r][g] = {} end
	if not c[r][g][b] then c[r][g][b] = {r = r, g = g, b = b} end
	if icon then text = "|T"..icon..":16:16:-3:0:256:256:20:235:20:235|t"..text end
	RaidNotice_AddMessage(RaidWarningFrame, text, c[r][g][b])
end

local function SkinDXEBar(bar)
	-- The main bar
	bar:SetTemplate("Transparent")
	bar.bg:SetTexture(nil)
	bar.border:Kill()
	bar.statusbar:SetStatusBarTexture(c["media"].normTex)
	bar.statusbar:ClearAllPoints()
	bar.statusbar:SetPoint("TOPLEFT",borderWidth, -borderWidth)
	bar.statusbar:SetPoint("BOTTOMRIGHT",-borderWidth, borderWidth)
	
	-- Right Icon
	bar.righticon:SetTemplate("Default")
	bar.righticon.border:Kill()
	bar.righticon.t:SetTexCoord(unpack(buttonZoom))
	bar.righticon.t:ClearAllPoints()
	bar.righticon.t:SetPoint("TOPLEFT", borderWidth, -borderWidth)
	bar.righticon.t:SetPoint("BOTTOMRIGHT", -borderWidth, borderWidth)
	bar.righticon.t:SetDrawLayer("ARTWORK")
	
	-- Left Icon
	bar.lefticon:SetTemplate("Default")
	bar.lefticon.border:Kill()
	bar.lefticon.t:SetTexCoord(unpack(buttonZoom))
	bar.lefticon.t:ClearAllPoints()
	bar.lefticon.t:SetPoint("TOPLEFT",borderWidth, -borderWidth)
	bar.lefticon.t:SetPoint("BOTTOMRIGHT",-borderWidth, borderWidth)
	bar.lefticon.t:SetDrawLayer("ARTWORK")
end

local function LoadSkin()

	--Kill DXE's skinning
	DXE.NotifyBarTextureChanged = c.noop
	DXE.NotifyBorderChanged = c.noop
	DXE.NotifyBorderColorChanged = c.noop
	DXE.NotifyBorderEdgeSizeChanged = c.noop
	DXE.NotifyBackgroundTextureChanged = c.noop
	DXE.NotifyBackgroundInsetChanged = c.noop
	DXE.NotifyBackgroundColorChanged = c.noop

	--Hook Window Creation
	DXE.CreateWindow_ = DXE.CreateWindow
	DXE.CreateWindow = function(self, name, width, height)
		local win = self:CreateWindow_(name, width, height)
		win:SetTemplate("Transparent")
		return win
	end

	-- Skin the pane
	DXE.Pane:SetTemplate("Transparent")

	-- Hook Health frames (Skin & spacing)
	DXE.LayoutHealthWatchers_ = DXE.LayoutHealthWatchers
	DXE.LayoutHealthWatchers = function(self)
		self.db.profile.Pane.BarSpacing = barSpacing
		self:LayoutHealthWatchers_()
		for i,hw in ipairs(self.HW) do
			if hw:IsShown() then
				hw:SetTemplate("Transparent")
				hw.border:Kill()
				hw.healthbar:SetStatusBarTexture(E["media"].normTex)
			end
		end
	end

	DXE.Alerts.RefreshBars_ = DXE.Alerts.RefreshBars
	DXE.Alerts.RefreshBars = function(self)
		if self.refreshing then return end
		self.refreshing = true
		self.db.profile.BarSpacing = barSpacing
		self.db.profile.IconXOffset = barSpacing
		self:RefreshBars_()
		local i = 1
		while _G["DXEAlertBar"..i] do
			local bar = _G["DXEAlertBar"..i]
			bar:SetScale(1)
			bar.SetScale = c.noop
			SkinDXEBar(bar)
			i = i + 1
		end
		self.refreshing = false
	end
	
	DXE.Alerts.Dropdown_ = DXE.Alerts.Dropdown
	DXE.Alerts.Dropdown = function(self,...)
		self:Dropdown_(...)
		self:RefreshBars()
	end

	DXE.Alerts.CenterPopup_ = DXE.Alerts.CenterPopup
	DXE.Alerts.CenterPopup = function(self,...)
		self:CenterPopup_(...)
		self:RefreshBars()
	end

	DXE.Alerts.Simple_ = DXE.Alerts.Simple
	DXE.Alerts.Simple = function(self,...)
		self:Simple_(...)
		self:RefreshBars()
	end	
	
	-- Force some updates
	DXE:LayoutHealthWatchers()
	DXE.Alerts:RefreshBars()
	DXE.Pane.border:Kill()	

	DXE.Pane.timer.left:FontTemplate(nil, 18)
	DXE.Pane.timer.right:FontTemplate(nil, 12)
	
	for i=1, #movers do
		if _G[movers[i]] then
			_G[movers[i]]:SetTemplate("Transparent")
		end
	end	

	local sink = LibStub:GetLibrary("LibSink-2.0")
	if sink and sink.handlers and sink.handlers.RaidWarning then
		sink.handlers.RaidWarning = SkinRWIcon
	end	
end

s:RegisterSkin('DXE', LoadSkin)
end)